﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using FineUIPro;

namespace CanDoo.Oil.WebApp.oilMediaAD
{
    public partial class Edit : EditPagebase<IMediaADContract, MediaAD>
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (PageMode != pagemode.Add)
                    LoadData();
            }
        }

        protected override void LoadData(MediaAD entity)
        {
            ed_Name.SetValue(entity.Name);
            ed_Url.SetValue(entity.Url);
            ed_Size.SetValue(entity.Size);
            ed_MD5.SetValue(entity.MD5);
            ed_StartTime.SetValue(entity.StartTime);
            ed_EndTime.SetValue(entity.EndTime);
            ed_Remark.SetValue(entity.Remark);
        }

        protected override OperationResult SaveData(MediaAD entity)
        {
            entity.Name = ed_Name.GetValue().ToString();
            entity.Url = ed_Url.GetValue().ToString();
            entity.Size = ed_Size.GetValue().ToDouble();
            entity.MD5 = ed_MD5.GetValue().ToString();
            entity.StartTime = ed_StartTime.GetValue().ToNullableDateTime();
            if(entity.StartTime ==DateTime.MinValue)
                entity.StartTime = null;
            entity.EndTime = ed_EndTime.GetValue().ToNullableDateTime();
            if(entity.EndTime ==DateTime.MinValue)
                entity.EndTime = null;
            entity.Remark = ed_Remark.GetValue();
            return base.SaveData(entity);
        }

        protected void ed_ADUploader_FileSelected(object sender, EventArgs e)
        {
            var result = UploadFile.UploadWithResult(this.ed_ADUploader);
            if (result.Status)
            {
                ed_MD5.SetValue(result.MD5);
                ed_Size.SetValue(result.FileSize);
                ed_Url.SetValue(result.FilePath);
                ed_ADUploader.Reset();
            }
            else
            {
                Alert.ShowInTop(result.ErrInfo);
            }
        }
    }
}
